//
//  WNSpringView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/9/24.
//

import UIKit

class WNVersionView: UIView {
    
    static let shared = WNVersionView()
    
    lazy var bgImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "discover_version_bg"))
        image.isUserInteractionEnabled = true
         return image
     }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 17)
        label.textColor = .init(named: "black_333333")
        label.textAlignment = .center
        return label
    }()
    
    lazy var refuseBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("晚點再說", for: .normal)
        btn.setTitleColor(.init(named: "gray_666666"), for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 14)
        btn.titleLabel?.textAlignment = .center
        btn.backgroundColor = .init(named: "white_F3F3F3")
        btn.rx.tap.bind { [weak self] in
            saveUpgradeClose(true)
            saveUpgradeDate(Date().timeDate)
            hidenVersion()
        }.disposed(by: rx.disposeBag)
        btn.wn_cornerWithBorder(18)
        return btn
    }()
    
    lazy var agreeBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("升級", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 14)
        btn.titleLabel?.textAlignment = .center
        btn.backgroundColor = .init(named: "red_FE95B4")
        btn.rx.tap.bind { [weak self] in
            saveUpgradeClose(true)
            saveUpgradeDate(Date().timeDate)
            let url = URL(string: "https://apps.apple.com/app/id6746667402")
            if let u = url, UIApplication.shared.canOpenURL(u) {
                UIApplication.shared.open(u, options: [:], completionHandler: { (success) in
                })
            }
            hidenVersion()
        }.disposed(by: rx.disposeBag)
        btn.wn_cornerWithBorder(18)
        return btn
    }()
    
    lazy var textView: IQTextView = {
        let view = IQTextView()
        view.font = .wn_font(name: .bold, size: 14)
        view.showsVerticalScrollIndicator = false
        view.textColor = .init(named: "black_333333")
        view.isEditable = false
        view.isSelectable = false
        view.backgroundColor = .clear
        return view
    }()
    
    var model: VersionModel?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .init(white: 0, alpha: 0.55)
        addSubview(bgImage)
        bgImage.addSubview(titleLabel)
        bgImage.addSubview(refuseBtn)
        bgImage.addSubview(agreeBtn)
        bgImage.addSubview(textView)
        bgImage.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalTo(275)
            make.height.equalTo(346)
        }
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(140)
        }
        refuseBtn.snp.makeConstraints { (make) in
            make.bottom.equalTo(-20)
            make.left.equalTo(15)
            make.size.equalTo(CGSize(width: 110, height: 36))
        }
        agreeBtn.snp.makeConstraints { (make) in
            make.bottom.equalTo(-20)
            make.right.equalTo(-15)
            make.size.equalTo(CGSize(width: 110, height: 36))
        }
        textView.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(4)
            make.left.equalTo(19)
            make.right.equalTo(-19)
            make.bottom.equalTo(agreeBtn.snp.top).offset(-8)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func showType(_ versionModel: VersionModel?) {
        if let m = versionModel {
            model = m
            titleLabel.text = m.title
            textView.text = m.desc?.replacingOccurrences(of: "&&&", with: "\n")
        }
    }
}

func showVersion(_ m: VersionModel?) {
    if let w = appDelegate.wn_window {
        WNVersionView.shared.frame = Screen.bounds
        WNVersionView.shared.showType(m)
        w.addSubview(WNVersionView.shared)
    }
}

func hidenVersion() {
    WNVersionView.shared.removeFromSuperview()
}
